package com.verisette.ncos.service.approver.bizunit;

import java.util.List;

import org.apache.commons.lang.NumberUtils;
import org.apache.log4j.Logger;
import org.corp.agi.common.bean.PagingBean;
import org.corp.agi.common.constant.CommonConstant;
import org.corp.agi.common.exception.ProcessException;
import org.corp.agi.service.bean.ProcessContext;
import org.corp.agi.service.bean.UserBean;
import org.corp.agi.service.bizunit.CommonBusinessUnit;

import com.verisette.ncos.common.ProjectConstant;
import com.verisette.ncos.persistence.domain.CustomCustomerOrder;

public class InquiryCustomerOrderBU extends CommonBusinessUnit{

	private Logger LOG = Logger.getLogger(InquiryCustomerOrderBU.class);
	
	@SuppressWarnings("unchecked")
	@Override
	public void execute(ProcessContext o) throws ProcessException 
	{
		int status = ProjectConstant.STATUS_WAIT_APPROVE;
		if(null != o.getValueObject())
		{
			status = NumberUtils.createInteger(String.valueOf(o
					.getValueObject()));
		}
		UserBean user = o.getUserBean();
		PagingBean paging = o.getPaging();
		if(LOG.isDebugEnabled())
		{
			LOG.debug("Processing InquiryCustomerOrderBU...");
			LOG.debug(String.format("User: %1$s", user.getUsername()));
		}
		
		StringBuilder sb = new StringBuilder();
//		sb.append("select a.order_id, a.mmb_id, a.mtp_id,")
		sb.append("select a.order_id, a.mtp_id,")
		.append(" a.status, a.approved_by,a.pdf_path, ")
//		.append(" b.mtp_path, c.mmb_fname_th,c.mmb_lname_th, ")
		.append(" b.mtp_path, c.*, ")
		.append(" d.branch_name_th, a.created_date, a.quantity ")
		.append(" from trans_customer_order a ")
		.append(" inner join master_template b on a.mtp_id=b.mtp_id ")
		.append(" inner join master_member c on a.mmb_id=c.mmb_id ")
		.append(" left join master_branch d on c.branch_id=d.branch_id ")
		//.append(" and upper(a.approved_by) = upper('"+user.getUsername()+"') ")
		.append(" where 1=1 ")
		.append(" and a.status = "+status);
		
		List<CustomCustomerOrder> list = (List<CustomCustomerOrder>) commonDAO
				.nativeQuery(sb.toString(), CustomCustomerOrder.class, paging);
		o.setValueObject(list);
		o.setPaging(paging);
		o.setStatus(CommonConstant.SERVICE_STATUS_SUCCESS);
	}

}
